package com.issac.json.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.text.SimpleDateFormat;

//全局配置JSON返回格式

@Configuration
public class WebMvcConfig {

    /*
     *  Gson:
     *       pom.xml导入Gson依赖，移除json依赖
     *       配置GsonHttpMessageConverter
     *       配置Gson对象
     *
     *  FastJson:
     *       pom.xml导入FastJson依赖
     *       配置FastJsonHttpMessageConverter
     * */
    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-hh");
        converter.setFastJsonConfig(fastJsonConfig);
        return converter;
    }
    //Jackson
    @Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy/mm/hh"));
        converter.setObjectMapper(objectMapper);
        return converter;
    }

    @Bean
    ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-mm-hh"));
        return objectMapper;
    }

}
